/**
 * 日期格式化方法
 * =============================================================
 * @param {String} value - 标准的格林尼治时间字符串
 * @param {String} [format] - 格式化样式字符串（常用的：yyyy-MM-dd、yyyy-MM-dd HH-mm、yyyy-MM-dd HH-mm-ss）
 * @returns {String}
 */
export function formatDate(value, format = 'yyyy-MM-dd HH:mm:ss') {
  const t = new Date(value)
  const date = {
    'M+': t.getMonth() + 1,
    'd+': t.getDate(),
    'H+': t.getHours(),
    'h+': t.getHours(),
    'm+': t.getMinutes(),
    's+': t.getSeconds(),
    'q+': Math.floor((t.getMonth() + 3) / 3),
    'S+': t.getMilliseconds()
  }

  if (/(y+)/i.test(format)) {
    format = format.replace(
      RegExp.$1,
      (t.getFullYear() + '').substr(4 - RegExp.$1.length)
    )
  }

  for (let k in date) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1
          ? date[k]
          : ('00' + date[k]).substr(('' + date[k]).length)
      )
    }
  }

  return format
}

// 格式化时间
export function dateFtt(fmt, date) { //author: meizz  
  var o = {
    "M+": date.getMonth() + 1, //月份   
    "d+": date.getDate(), //日   
    "h+": date.getHours(), //小时   
    "m+": date.getMinutes(), //分   
    "s+": date.getSeconds(), //秒   
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
    "S": date.getMilliseconds() //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return Math.round(fmt / 1000);
}

// 格式化时间
export function formatDate(date, pattern) {
  if (date == undefined) {
    date = new Date();
  }
  if (pattern == undefined) {
    pattern = "yyyy-MM-dd hh:mm:ss";
  }

  var o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
    "q+": Math.floor((date.getMonth() + 3) / 3),
    "S": date.getMilliseconds()
  };
  if (/(y+)/.test(pattern)) {
    pattern = pattern.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  }

  for (var k in o) {
    if (new RegExp("(" + k + ")").test(pattern)) {
      pattern = pattern.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }

  return pattern;
}

export function duration(start, end = new Date()) {
  const diff = new Date(end).getTime() - new Date(start).getTime()

  let seconds = diff
  let minutes = Math.floor(diff / (60 * 1000))
  let hours = Math.floor(diff / (3600 * 1000))
  let days = Math.floor(diff / (24 * 3600 * 1000))
  let weeks = Math.floor(diff / (24 * 3600 * 1000 * 7))
  let months = Math.floor(diff / (24 * 3600 * 1000 * 30))
  let years = Math.floor(diff / (24 * 3600 * 1000 * 365))

  return {
    seconds: seconds,
    minutes: minutes,
    hours: hours,
    days: days,
    weeks: weeks,
    months: months,
    years: years
  }
}

export function toDuration(millis) {
  const minutes = Math.floor(millis / 60000)
  const seconds = ((millis % 60000) / 1000).toFixed(0)
  return (minutes > 0 ? minutes + '分' : '') + seconds + '秒'
}

export function formatDateDescribe(time) {
  let minute = 1000 * 60
  let hour = minute * 60
  let day = hour * 24
  // let halfamonth = day * 15
  // let month = day * 30
  let dateTimeStamp = new Date(time).getTime()
  let now = new Date().getTime()
  let diffValue = now - dateTimeStamp

  // let monthC = diffValue / month
  // let weekC = diffValue / (7 * day)
  let dayC = diffValue / day
  let hourC = diffValue / hour
  let minC = diffValue / minute
  let result
  if (dayC >= 7) {
    result = formatDate(time, 'yyyy-MM-dd HH:mm:ss')
  } else if (dayC >= 1) {
    result = parseInt(dayC) + '天前'
  } else if (hourC >= 1) {
    result = parseInt(hourC) + '小时前'
  } else if (minC >= 1) {
    result = parseInt(minC) + '分钟前'
  } else result = '刚刚发表'
  return result
}
